package com.example.thread.case5;

/**
 * 确保线程互斥的访问同步代码
 */
public class Counter3 {
    public static int inc = 0;

    public synchronized void increase(){
        inc++;
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            inc = 0;
            count();
        }
    }

    public static void count() throws InterruptedException {
        Counter3 c3 = new Counter3();
        Thread th = null;
        Long t1 = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            th = new Thread(){
                @Override
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        c3.increase();
                    }
                }
            };
            th.start();;
            th.join();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Counter1 , "+String.format("结果：%s,耗时(ms)：%s", c3.inc, (t2 - t1)));
    }
}
